import java.io.FileNotFoundException;
import java.util.ArrayList;


public class SimScheduler {
	
	static boolean statsOnlyFlag = false;
	static boolean fcfsFlag = false;
	static boolean rr1Flag = false;
	static boolean rr2Flag = false;
	static boolean mlfqFlag = false;
	
	static String inFile;
	static String outFile;
	
	public static void main(String[] args) {

		//Make sure our arguments are valid and set the flags
		if (!processArguments(args))
			return;
				
		//load the process list
		ArrayList<Process> loadedProcesses = null;
		try {
			loadedProcesses = Process.LoadProcesses(inFile);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (fcfsFlag)
		{
			//Scheduling Algorithm initiated inside OS Simulator
//			SchedulingAlgorithm fcfs = new FCFS();
			OSSimulator fcfs_os = new OSSimulator("fcfs", loadedProcesses);

			fcfs_os.Run();
			fcfs_os.PrintStatistics();
		}
		
		Process.ResetProcessList(loadedProcesses);
		
		if (rr1Flag)
		{
//			SchedulingAlgorithm rr1 = new RoundRobin(10);
			OSSimulator rr1_os = new OSSimulator("rr1", loadedProcesses);
			
			rr1_os.Run();
			rr1_os.PrintStatistics();
		}
		
		Process.ResetProcessList(loadedProcesses);
		
		if (rr2Flag)
		{
//			SchedulingAlgorithm rr2 = new RoundRobin(50);
			OSSimulator rr2_os = new OSSimulator("rr2", loadedProcesses);
			
			rr2_os.Run();
			rr2_os.PrintStatistics();
		}
		
		Process.ResetProcessList(loadedProcesses);
		
		if (mlfqFlag)
		{
//			SchedulingAlgorithm mlfq = new MLFQ();
			OSSimulator mlfq_os = new OSSimulator("mlfq", loadedProcesses);
			
			mlfq_os.Run();
			mlfq_os.PrintStatistics();
		}
		
		//done
	}
	
	protected static boolean processArguments(String[] args)
	{
		if (args.length < 2)
		{			//no input and output file
			System.out.println("Invalid input or output files.");
			return false;
		}
		inFile = args[0];
		outFile = args[1];
		
		for (int i = 2; i < args.length; i++)
		{
			if (args[i].equals("-statsOnly"))
				statsOnlyFlag = true;
			else if (args[i].equals("-fcfs"))
				fcfsFlag = true;
			else if (args[i].equals("-rr1"))
				rr1Flag = true;
			else if (args[i].equals("-rr2"))
				rr2Flag = true;
			else if (args[i].equals("-mlfq"))
				mlfqFlag = true;
		}
		
		if (!fcfsFlag && !rr1Flag && !rr2Flag && !mlfqFlag)
		{
			
			fcfsFlag = rr1Flag = rr2Flag = mlfqFlag = true;
		}
		
		
		
		
		
		
		return true;
	}
}
